package com.xujl.demo;

import android.os.Bundle;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.changan.remote.assistant.IAssisantCallback;
import com.changan.remote.assistant.RemoteAssistantManager;
import com.xujl.datalibrary.bean.RequestBean;
import com.xujl.datalibrary.bean.RequestLoginBean;
import com.xujl.datalibrary.task.TaskType;
import com.xujl.datalibrary.util.JsonParseUtils;

public class MainActivity extends AppCompatActivity {
    private EditText etName;
    private EditText etPass;
    private TextView tvResult;

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etName = findViewById(R.id.et_name);
        etPass = findViewById(R.id.et_pass);
        tvResult = findViewById(R.id.tv_result);
        findViewById(R.id.btn).setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick (View v) {
                final RequestBean requestBean = new RequestBean();
                requestBean.setRequestCode(666);
                requestBean.setParamsJson(JsonParseUtils.toJson(
                        new RequestLoginBean(etName.getText().toString(), etPass.getText().toString())));
                requestBean.setTaskType(TaskType.NETWORK);
                RemoteAssistantManager.getInstance().sendEvent(JsonParseUtils.toJson(requestBean));
            }
        });
        RemoteAssistantManager.getInstance().registerListener(mIAssisantCallback);

    }

    @Override
    protected void onDestroy () {
        RemoteAssistantManager.getInstance().unregisterListener(mIAssisantCallback);
        super.onDestroy();
    }

    IAssisantCallback.Stub mIAssisantCallback = new IAssisantCallback.Stub() {
        @Override
        public void onCallback (final String json) throws RemoteException {
            tvResult.post(new Runnable() {
                @Override
                public void run () {
                    tvResult.setText(json);
                }
            });
        }

        @Override
        public int getRequestCode () throws RemoteException {
            return 666;
        }
    };

}
